package window;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

import main.DataConnection;

import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;

/**
 * Window created to remove elements from DB. Can remove either problems or ITS. We choose its mode by specifying it in constructor {@code type} parameter.
 * 
 * @author Dawid Nejman
 * @version 1.0 19-12-2011
 */
public class RemoveElementWindow extends JFrame {

	private static final long serialVersionUID = -7175568553267658125L;
	public static final int PROBLEM = 0;
	public static final int ITS = 1;
	private final int mode;
	private JButton jButton1;
	private JComboBox<String> jComboBox1;
	private JLabel jLabel1;

	/**
	 * Creates new form RemoveElementWindow with given mode. Mode can be {@code PROBLEM} or {@code ITS}.
	 * 
	 * @param type
	 *            window mode.
	 */
	public RemoveElementWindow(int type) {
		mode = type;
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 */
	private void initComponents() {
		jLabel1 = new JLabel();
		jComboBox1 = new JComboBox<String>();
		jButton1 = new JButton();

		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setResizable(false);

		if (mode == PROBLEM) {
			jLabel1.setText("Problem");
			String[] problemNames = DataConnection.getProblemNames();
			jComboBox1.setModel(new DefaultComboBoxModel<String>(problemNames));
		} else if (mode == ITS) {
			jLabel1.setText("ITS");
			String[] itsNames = DataConnection.getItsNames();
			jComboBox1.setModel(new DefaultComboBoxModel<String>(itsNames));
		}

		jButton1.setText("Remove");
		jButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.LEADING).add(
				layout.createSequentialGroup()
						.addContainerGap()
						.add(layout
								.createParallelGroup(GroupLayout.LEADING)
								.add(layout.createSequentialGroup().add(jLabel1).add(0, 0, Short.MAX_VALUE))
								.add(layout
										.createSequentialGroup()
										.add(layout.createParallelGroup(GroupLayout.LEADING).add(jComboBox1, 0, 259, Short.MAX_VALUE)
												.add(GroupLayout.TRAILING, layout.createSequentialGroup().add(0, 0, Short.MAX_VALUE).add(jButton1)))
										.addContainerGap()))));
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.LEADING).add(
				layout.createSequentialGroup().addContainerGap().add(jLabel1).addPreferredGap(LayoutStyle.RELATED)
						.add(jComboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).add(jButton1).addContainerGap()));

		pack();
	}

	private void jButton1ActionPerformed(ActionEvent evt) {
		if (mode == ITS) {
			int index = jComboBox1.getSelectedIndex();
			String itsName = jComboBox1.getItemAt(index);
			DataConnection.removeIts(itsName);
		} else if (mode == PROBLEM) {
			int index = jComboBox1.getSelectedIndex();
			String problemName = jComboBox1.getItemAt(index);
			DataConnection.removeProblem(problemName);
		}
		dispose();
	}
}
